package com.hyh.mall.controller.sys;

import com.hyh.mall.common.Counts;
import com.hyh.mall.controller.common.CommonController;
import com.hyh.mall.domin.MUser;
import com.hyh.mall.service.IUserService;
import com.hyh.mall.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/sys/user")
public class UserController extends CommonController {

//    @Resource
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    public String login(MUser mUser){
        return"login";
    }

    @RequestMapping("/register")
    public String register(MUser mUser){
        return"register";
    }

    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request){
        request.getSession().invalidate();
        log.debug("logOut");
        return "login";
    }

    @RequestMapping(value = "/login_mall")
    public String login_mall(HttpServletRequest request, String userName, MUser mUser){
        log.debug("login_mall");
        mUser.setStatus(Counts.USER_STATUS_ENABLE);
        //用户存在，将用户放入session
        System.out.println("login####" +mUser);
        //从数据库查user password
        MUser mUserTemp=userService.queryUser(mUser);
        if(null !=mUserTemp){
            request.getSession().setAttribute("mUser",mUserTemp);
            return "index";
        }
//        request.getSession().setAttribute("mUser",mUser);
        request.setAttribute("msg","用户不存在");
        return"login";
    }

    @RequestMapping(value = "/register_mall")
    public String regist_mall(HttpServletRequest request, String userName, MUser mUser) {
        log.debug("login_mall");
        mUser.setStatus(Counts.USER_STATUS_ENABLE);
        MUser mUserTemp = userService.queryUserByName(mUser);
        if(null !=mUserTemp){
            request.setAttribute("msg","用户已存在");
            return "register";
        }
        userService.saveUser(mUser);
        request.setAttribute("msg","注册成功");
        return "register";
    }
}
